1 module hip.windowing.platforms.x11lib.x11;
2 
3 version(Android){}
4 else version(linux)
5     version = X11;
6 version(X11):
7 
8 import hip.windowing.platforms.x11lib.glx;
9 import core.stdc.config;
10 
11 extern(C):
12 
13 
14 /**
15 *   I'll define here a minimal set of x11 functions.
16 */
17 alias XID = c_ulong;
18 alias Atom = c_ulong;
19 alias VisualID = c_ulong;
20 alias Time = c_ulong;
21 
22 struct _XGC;
23 alias GC = _XGC*;
24 struct Depth;
25 struct ScreenFormat;
26 struct _XrmHashBucketRec;
27 struct _XPrivate;
28 struct XExtData;
29 alias XErrorHandler = extern(C) nothrow @nogc int function(Display*, XErrorEvent*);
30 struct _XDisplay
31 {
32 	import core.stdc.config:c_ulong;
33     XExtData* ext_data;                                 /* hook for extension to hang data                              */
34     _XPrivate* private1;
35     int fd;                                             /* Network socket.                                              */
36     int private2;
37     int proto_major_version;                            /* major version of server's X protocol */
38     int proto_minor_version;                            /* minor version of servers X protocol */
39     char* vendor;                                       /* vendor of the server hardware */
40     XID private3;
41     XID private4;
42     XID private5;
43     int private6;
44     extern (C) nothrow XID function(_XDisplay*) resource_alloc;             /* allocator function */
45     int char_order;                                     /* screen char order, LSBFirst, MSBFirst */
46     int bitmap_unit;                                    /* padding and data requirements */
47     int bitmap_pad;                                     /* padding requirements on bitmaps */
48     int bitmap_bit_order;                               /* LeastSignificant or MostSignificant */
49     int nformats;                                       /* number of pixmap formats in list */
50     ScreenFormat* pixmap_format;                        /* pixmap format list */
51     int private8;
52     int release;                                        /* release of the server */
53     _XPrivate* private9, private10;
54     int qlen;                                           /* Length of input event queue */
55     c_ulong  last_request_read;                         /* seq number of last event read */
56     c_ulong  request;                                   /* sequence number of last request. */
57     XPointer private11;
58     XPointer private12;
59     XPointer private13;
60     XPointer private14;
61     uint max_request_size;                          /* maximum number 32 bit words in request*/
62     _XrmHashBucketRec* db;
63     extern (C) nothrow int function( _XDisplay* )private15;
64     char* display_name;                             /* "host:display" string used on this connect*/
65     int default_screen;                             /* default screen for operations */
66     int nscreens;                                   /* number of screens on this server*/
67     Screen* screens;                                /* pointer to list of screens */
68     c_ulong motion_buffer;                          /* size of motion buffer */
69     c_ulong private16;
70     int min_keycode;                                /* minimum defined keycode */
71     int max_keycode;                                /* maximum defined keycode */
72     XPointer private17;
73     XPointer private18;
74     int private19;
75     char* xdefaults;                                /* contents of defaults from server */
76     /* there is more to this structure, but it is private to Xlib */
77 }
78 alias Display = _XDisplay;
79 alias _XPrivDisplay = _XDisplay*;
80 
81 struct Screen
82 {
83     XExtData *ext_data;             /* hook for extension to hang data */
84     Display* display;      /* back pointer to display structure */
85     Window root;                    /* root window ID */
86     int width, height;              /* width and height of screen */
87     int mwidth, mheight;            /* width and height of in millimeters */
88     int ndepths;                    /* number of depths possible */
89     Depth *depths;                  /* list of allowable depths on the screen */
90     int root_depth;                 /* bits per pixel */
91     Visual *root_visual;            /* root visual */
92     GC default_gc;                  /* GC for the root root visual */
93     Colormap cmap;                  /* default colormap */
94     uint white_pixel;
95     uint black_pixel;      /* white and black pixel values */
96     int max_maps, min_maps;         /* max and min colormaps */
97     int backing_store;              /* Never, WhenMapped, Always */
98     Bool save_unders;
99     long root_input_mask;           /* initial root input mask */
100 }
101 
102 struct XVisualInfo
103 {
104  	Visual* visual;
105  	VisualID visualid;
106  	int screen_num;
107  	uint depth;
108  	int class_;
109  	c_ulong  red_mask;  
110  	c_ulong  green_mask;  
111 	c_ulong  blue_mask;  
112  	int colormap_size; /* Same as map_entries member of Visual */
113  	int bits_per_rgb;
114 }
115 
116 struct Visual;
117 alias Status = int;
118 alias KeyCode = ubyte;
119 alias KeySym = XID;
120 alias Bool = int;
121 alias XPointer = char*;
122 alias Drawable = XID;
123 alias Cursor = XID;
124 alias Colormap = XID;
125 alias Pixmap = XID;
126 alias Window = XID;
127 enum None = 0;
128 enum True = 1;
129 enum False = 0;
130 enum AllocNone = 0;
131 enum AllocAll = 1;
132 
133 /*****************************************************************
134  * ERROR CODES 
135  *****************************************************************/
136 
137 enum Success		= 0;	/* everything's okay */
138 enum BadRequest	    = 1;	/* bad request code */
139 enum BadValue	    = 2;	/* int parameter out of range */
140 enum BadWindow	    = 3;	/* parameter not a Window */
141 enum BadPixmap	    = 4;	/* parameter not a Pixmap */
142 enum BadAtom		= 5;	/* parameter not an Atom */
143 enum BadCursor	    = 6;	/* parameter not a Cursor */
144 enum BadFont		= 7;	/* parameter not a Font */
145 enum BadMatch	    = 8;	/* parameter mismatch */
146 enum BadDrawable	= 9;	/* parameter not a Pixmap or Window */
147 enum BadAccess	    = 10;	/* depending on context:
148 				             - key/button already grabbed
149 				             - attempt to free an illegal 
150 				               cmap entry 
151 				            - attempt to store into a read-only 
152 				               color map entry.
153  				            - attempt to modify the access control
154 				               list from other than the local host.
155 				            */
156 enum BadAlloc	       = 11;	/* insufficient resources */
157 enum BadColor	       = 12;	/* no such colormap */
158 enum BadGC		       = 13;	/* parameter not a GC */
159 enum BadIDChoice	   = 14;	/* choice not in range or already used */
160 enum BadName		   = 15;	/* font or color name doesn't exist */
161 enum BadLength	       = 16;	/* Request length incorrect */
162 enum BadImplementation = 17;	/* server is defective */
163 
164 auto ScreenOfDisplay(Display* dpy, int scr){return &(cast(_XPrivDisplay)(dpy)).screens[scr];}
165 auto DefaultScreenOfDisplay(Display* dpy){return ScreenOfDisplay(dpy,DefaultScreen(dpy));}
166 auto DisplayOfScreen(Screen* s){return s.display;}
167 
168 auto BlackPixel(Display* dpy, int  scr)
169 {
170     return ScreenOfDisplay(dpy,scr).black_pixel;
171 }
172 auto WhitePixel(Display* dpy, int scr)
173 {
174     return ScreenOfDisplay(dpy,scr).white_pixel;
175 }
176 auto DefaultScreen(Display* dpy){return (cast(_XPrivDisplay)dpy).default_screen;}
177 auto RootWindow(Display* dpy, int scr){return ScreenOfDisplay(dpy,scr).root;}
178 auto RootWindowOfScreen(Screen* s){return s.root;}
179 
180 
181 struct XSetWindowAttributes 
182 {
183 	Pixmap background_pixmap;	/* background, None, or ParentRelative */
184 	c_ulong  background_pixel;	/* background pixel */ 
185 	Pixmap border_pixmap;		/* border of the window or CopyFromParent */
186 	c_ulong  border_pixel;	/* border pixel value */ 
187 	int bit_gravity;		/* one of bit gravity values */
188 	int win_gravity;		/* one of the window gravity values */
189 	int backing_store;		/* NotUseful, WhenMapped, Always */
190 	c_ulong  backing_planes;	/* planes to be preserved if possible */ 
191 	c_ulong  backing_pixel;	/* value to use in restoring planes */ 
192 	Bool save_under;		/* should bits under be saved? (popups) */
193 	long event_mask;		/* set of events that should be saved */
194 	long do_not_propagate_mask;	/* set of events that should not propagate */
195 	Bool override_redirect;		/* boolean value for override_redirect */
196 	Colormap colormap;		/* color map to be associated with window */
197 	Cursor cursor;			/* cursor to be displayed (or None) */
198 }
199 
200 
201 struct XWindowAttributes
202 {
203     int x, y;
204     /* location of window */
205     int width, height;
206     /* width and height of window */
207     int border_width;
208     /* border width of window */
209     int depth;
210     /* depth of window */
211     Visual *visual;
212     /* the associated visual structure */
213     Window root;
214     /* root of screen containing window */
215     int class_;
216     /* InputOutput, InputOnly*/
217     int bit_gravity;
218     /* one of the bit gravity values */
219     int win_gravity;
220     /* one of the window gravity values */
221     int backing_store;
222     /* NotUseful, WhenMapped, Always */
223     c_ulong  backing_planes;/* planes to be preserved if possible */ 
224     c_ulong  backing_pixel;/* value to be used when restoring planes */ 
225     Bool save_under;
226     /* boolean, should bits under be saved? */
227     Colormap colormap;
228     /* color map to be associated with window */
229     Bool map_installed;
230     /* boolean, is color map currently installed*/
231     int map_state;
232     /* IsUnmapped, IsUnviewable, IsViewable */
233     long all_event_masks;
234     /* set of events all people have interest in*/
235     long your_event_mask;
236     /* my event mask */
237     long do_not_propagate_mask;/* set of events that should not propagate */
238     Bool override_redirect;
239     /* boolean value for override-redirect */
240     Screen *screen;
241 
242     /* back pointer to correct screen */
243 
244 }
245 struct XWindowChanges
246 {
247     int x, y;
248     int width, height;
249     int border_width;
250     Window sibling;
251     int stack_mode;
252 }
253 
254 /*
255  * Definitions of specific events.
256  */
257 struct XKeyEvent
258 {
259 	int type;		/* of event */
260 	c_ulong  serial;	/* # of last request processed by server */ 
261 	Bool send_event;	/* true if this came from a SendEvent request */
262 	Display *display;	/* Display the event was read from */
263 	Window window;	        /* "event" window it is reported relative to */
264 	Window root;	        /* root window that the event occurred on */
265 	Window subwindow;	/* child window */
266 	Time time;		/* milliseconds */
267 	int x, y;		/* pointer x, y coordinates in event window */
268 	int x_root, y_root;	/* coordinates relative to root */
269 	uint state;	/* key or button mask */
270 	uint keycode;	/* detail */
271 	Bool same_screen;	/* same screen flag */
272 }
273 alias  XKeyPressedEvent = XKeyEvent;
274 alias  XKeyReleasedEvent = XKeyEvent;
275 enum ShiftMask   =     1; // Shift
276 enum LockMask    =     2; // Caps Lock
277 enum ControlMask =     4; // Ctrl
278 enum Mod1Mask    =     8; // Alt
279 enum Mod2Mask    =    16; // Num Lock
280 enum Mod3Mask    =    32; // Scroll Lock
281 enum Mod4Mask    =    64; // Windows
282 enum Mod5Mask    =   128; // ???
283 
284 
285 struct XButtonEvent
286 {
287 	int type;		/* of event */
288 	c_ulong  serial;	/* # of last request processed by server */ 
289 	Bool send_event;	/* true if this came from a SendEvent request */
290 	Display *display;	/* Display the event was read from */
291 	Window window;	        /* "event" window it is reported relative to */
292 	Window root;	        /* root window that the event occurred on */
293 	Window subwindow;	/* child window */
294 	Time time;		/* milliseconds */
295 	int x, y;		/* pointer x, y coordinates in event window */
296 	int x_root, y_root;	/* coordinates relative to root */
297 	uint state;	/* key or button mask */
298 	uint button;	/* detail */
299 	Bool same_screen;	/* same screen flag */
300 }
301 alias XButtonPressedEvent = XButtonEvent;
302 alias XButtonReleasedEvent = XButtonEvent;
303 
304 struct XMotionEvent 
305 {
306 	int type;		/* of event */
307 	c_ulong  serial;	/* # of last request processed by server */ 
308 	Bool send_event;	/* true if this came from a SendEvent request */
309 	Display *display;	/* Display the event was read from */
310 	Window window;	        /* "event" window reported relative to */
311 	Window root;	        /* root window that the event occurred on */
312 	Window subwindow;	/* child window */
313 	Time time;		/* milliseconds */
314 	int x, y;		/* pointer x, y coordinates in event window */
315 	int x_root, y_root;	/* coordinates relative to root */
316 	uint state;	/* key or button mask */
317 	char is_hint;		/* detail */
318 	Bool same_screen;	/* same screen flag */
319 }
320 alias XPointerMovedEvent = XMotionEvent;
321 
322 struct XCrossingEvent {
323 	int type;		/* of event */
324 	c_ulong  serial;	/* # of last request processed by server */ 
325 	Bool send_event;	/* true if this came from a SendEvent request */
326 	Display *display;	/* Display the event was read from */
327 	Window window;	        /* "event" window reported relative to */
328 	Window root;	        /* root window that the event occurred on */
329 	Window subwindow;	/* child window */
330 	Time time;		/* milliseconds */
331 	int x, y;		/* pointer x, y coordinates in event window */
332 	int x_root, y_root;	/* coordinates relative to root */
333 	int mode;		/* NotifyNormal, NotifyGrab, NotifyUngrab */
334 	int detail;
335 	/*
336 	 * NotifyAncestor, NotifyVirtual, NotifyInferior,
337 	 * NotifyNonlinear,NotifyNonlinearVirtual
338 	 */
339 	Bool same_screen;	/* same screen flag */
340 	Bool focus;		/* boolean focus */
341 	uint state;	/* key or button mask */
342 }
343 alias XEnterWindowEvent =  XCrossingEvent;
344 alias XLeaveWindowEvent =  XCrossingEvent;
345 
346 struct XFocusChangeEvent {
347 	int type;		/* FocusIn or FocusOut */
348 	c_ulong  serial;	/* # of last request processed by server */ 
349 	Bool send_event;	/* true if this came from a SendEvent request */
350 	Display *display;	/* Display the event was read from */
351 	Window window;		/* window of event */
352 	int mode;		/* NotifyNormal, NotifyWhileGrabbed,
353 				   NotifyGrab, NotifyUngrab */
354 	int detail;
355 	/*
356 	 * NotifyAncestor, NotifyVirtual, NotifyInferior,
357 	 * NotifyNonlinear,NotifyNonlinearVirtual, NotifyPointer,
358 	 * NotifyPointerRoot, NotifyDetailNone
359 	 */
360 }
361 alias XFocusInEvent = XFocusChangeEvent;
362 alias XFocusOutEvent = XFocusChangeEvent;
363 
364 /* generated on EnterWindow and FocusIn  when KeyMapState selected */
365 struct XKeymapEvent
366 {
367 	int type;
368 	c_ulong  serial;	/* # of last request processed by server */ 
369 	Bool send_event;	/* true if this came from a SendEvent request */
370 	Display *display;	/* Display the event was read from */
371 	Window window;
372 	char[32] key_vector;
373 }
374 
375 struct XExposeEvent
376 {
377 	int type;
378 	c_ulong  serial;	/* # of last request processed by server */ 
379 	Bool send_event;	/* true if this came from a SendEvent request */
380 	Display *display;	/* Display the event was read from */
381 	Window window;
382 	int x, y;
383 	int width, height;
384 	int count;		/* if non-zero, at least this many more */
385 }
386 
387 struct XGraphicsExposeEvent
388 {
389 	int type;
390 	c_ulong  serial;	/* # of last request processed by server */ 
391 	Bool send_event;	/* true if this came from a SendEvent request */
392 	Display *display;	/* Display the event was read from */
393 	Drawable drawable;
394 	int x, y;
395 	int width, height;
396 	int count;		/* if non-zero, at least this many more */
397 	int major_code;		/* core is CopyArea or CopyPlane */
398 	int minor_code;		/* not defined in the core */
399 }
400 
401 struct XNoExposeEvent
402 {
403 	int type;
404 	c_ulong  serial;	/* # of last request processed by server */ 
405 	Bool send_event;	/* true if this came from a SendEvent request */
406 	Display *display;	/* Display the event was read from */
407 	Drawable drawable;
408 	int major_code;		/* core is CopyArea or CopyPlane */
409 	int minor_code;		/* not defined in the core */
410 }
411 
412 struct XVisibilityEvent
413 {
414 	int type;
415 	c_ulong  serial;	/* # of last request processed by server */ 
416 	Bool send_event;	/* true if this came from a SendEvent request */
417 	Display *display;	/* Display the event was read from */
418 	Window window;
419 	int state;		/* Visibility state */
420 }
421 
422 struct XCreateWindowEvent
423 {
424 	int type;
425 	c_ulong  serial;	/* # of last request processed by server */ 
426 	Bool send_event;	/* true if this came from a SendEvent request */
427 	Display *display;	/* Display the event was read from */
428 	Window parent;		/* parent of the window */
429 	Window window;		/* window id of window created */
430 	int x, y;		/* window location */
431 	int width, height;	/* size of window */
432 	int border_width;	/* border width */
433 	Bool override_redirect;	/* creation should be overridden */
434 }
435 
436 struct XDestroyWindowEvent
437 {
438 	int type;
439 	c_ulong  serial;	/* # of last request processed by server */ 
440 	Bool send_event;	/* true if this came from a SendEvent request */
441 	Display *display;	/* Display the event was read from */
442 	Window event;
443 	Window window;
444 }
445 
446 struct XUnmapEvent
447 {
448 	int type;
449 	c_ulong  serial;	/* # of last request processed by server */ 
450 	Bool send_event;	/* true if this came from a SendEvent request */
451 	Display *display;	/* Display the event was read from */
452 	Window event;
453 	Window window;
454 	Bool from_configure;
455 }
456 
457 struct XMapEvent
458 {
459 	int type;
460 	c_ulong  serial;	/* # of last request processed by server */ 
461 	Bool send_event;	/* true if this came from a SendEvent request */
462 	Display *display;	/* Display the event was read from */
463 	Window event;
464 	Window window;
465 	Bool override_redirect;	/* boolean, is override set... */
466 }
467 
468 struct XMapRequestEvent
469 {
470 	int type;
471 	c_ulong  serial;	/* # of last request processed by server */ 
472 	Bool send_event;	/* true if this came from a SendEvent request */
473 	Display *display;	/* Display the event was read from */
474 	Window parent;
475 	Window window;
476 }
477 
478 struct XReparentEvent
479 {
480 	int type;
481 	c_ulong  serial;	/* # of last request processed by server */ 
482 	Bool send_event;	/* true if this came from a SendEvent request */
483 	Display *display;	/* Display the event was read from */
484 	Window event;
485 	Window window;
486 	Window parent;
487 	int x, y;
488 	Bool override_redirect;
489 }
490 
491 struct XConfigureEvent
492 {
493 	int type;
494 	c_ulong  serial;	/* # of last request processed by server */ 
495 	Bool send_event;	/* true if this came from a SendEvent request */
496 	Display *display;	/* Display the event was read from */
497 	Window event;
498 	Window window;
499 	int x, y;
500 	int width, height;
501 	int border_width;
502 	Window above;
503 	Bool override_redirect;
504 }
505 
506 struct XGravityEvent
507 {
508 	int type;
509 	c_ulong  serial;	/* # of last request processed by server */ 
510 	Bool send_event;	/* true if this came from a SendEvent request */
511 	Display *display;	/* Display the event was read from */
512 	Window event;
513 	Window window;
514 	int x, y;
515 }
516 
517 struct XResizeRequestEvent
518 {
519 	int type;
520 	c_ulong  serial;	/* # of last request processed by server */ 
521 	Bool send_event;	/* true if this came from a SendEvent request */
522 	Display *display;	/* Display the event was read from */
523 	Window window;
524 	int width, height;
525 }
526 
527 struct XConfigureRequestEvent
528 {
529 	int type;
530 	c_ulong  serial;	/* # of last request processed by server */ 
531 	Bool send_event;	/* true if this came from a SendEvent request */
532 	Display *display;	/* Display the event was read from */
533 	Window parent;
534 	Window window;
535 	int x, y;
536 	int width, height;
537 	int border_width;
538 	Window above;
539 	int detail;		/* Above, Below, TopIf, BottomIf, Opposite */
540 	c_ulong  value_mask; 
541 }
542 
543 struct XCirculateEvent
544 {
545 	int type;
546 	c_ulong  serial;	/* # of last request processed by server */ 
547 	Bool send_event;	/* true if this came from a SendEvent request */
548 	Display *display;	/* Display the event was read from */
549 	Window event;
550 	Window window;
551 	int place;		/* PlaceOnTop, PlaceOnBottom */
552 }
553 
554 struct XCirculateRequestEvent
555 {
556 	int type;
557 	c_ulong  serial;	/* # of last request processed by server */ 
558 	Bool send_event;	/* true if this came from a SendEvent request */
559 	Display *display;	/* Display the event was read from */
560 	Window parent;
561 	Window window;
562 	int place;		/* PlaceOnTop, PlaceOnBottom */
563 }
564 
565 struct XPropertyEvent
566 {
567 	int type;
568 	c_ulong  serial;	/* # of last request processed by server */ 
569 	Bool send_event;	/* true if this came from a SendEvent request */
570 	Display *display;	/* Display the event was read from */
571 	Window window;
572 	Atom atom;
573 	Time time;
574 	int state;		/* NewValue, Deleted */
575 }
576 
577 struct XSelectionClearEvent
578 {
579 	int type;
580 	c_ulong  serial;	/* # of last request processed by server */ 
581 	Bool send_event;	/* true if this came from a SendEvent request */
582 	Display *display;	/* Display the event was read from */
583 	Window window;
584 	Atom selection;
585 	Time time;
586 }
587 
588 struct XSelectionRequestEvent
589 {
590 	int type;
591 	c_ulong  serial;	/* # of last request processed by server */ 
592 	Bool send_event;	/* true if this came from a SendEvent request */
593 	Display *display;	/* Display the event was read from */
594 	Window owner;
595 	Window requestor;
596 	Atom selection;
597 	Atom target;
598 	Atom property;
599 	Time time;
600 }
601 
602 struct XSelectionEvent
603 {
604 	int type;
605 	c_ulong  serial;	/* # of last request processed by server */ 
606 	Bool send_event;	/* true if this came from a SendEvent request */
607 	Display *display;	/* Display the event was read from */
608 	Window requestor;
609 	Atom selection;
610 	Atom target;
611 	Atom property;		/* ATOM or None */
612 	Time time;
613 }
614 
615 struct XColormapEvent
616 {
617 	int type;
618 	c_ulong  serial;	/* # of last request processed by server */ 
619 	Bool send_event;	/* true if this came from a SendEvent request */
620 	Display *display;	/* Display the event was read from */
621 	Window window;
622 	Colormap colormap;	/* COLORMAP or None */
623 	Bool c_new;
624 	int state;		/* ColormapInstalled, ColormapUninstalled */
625 }
626 
627 struct XClientMessageEvent
628 {
629 	int type;
630 	c_ulong  serial;	/* # of last request processed by server */ 
631 	Bool send_event;	/* true if this came from a SendEvent request */
632 	Display *display;	/* Display the event was read from */
633 	Window window;
634 	Atom message_type;
635 	int format;
636 	union DataUnion {
637 		char[20] b;
638 		short[10] s;
639 		long[5] l;
640     }
641     DataUnion data;
642 
643 }
644 
645 struct XMappingEvent
646 {
647 	int type;
648 	c_ulong  serial;	/* # of last request processed by server */ 
649 	Bool send_event;	/* true if this came from a SendEvent request */
650 	Display *display;	/* Display the event was read from */
651 	Window window;		/* unused */
652 	int request;		/* one of MappingModifier, MappingKeyboard,
653 				   MappingPointer */
654 	int first_keycode;	/* first keycode */
655 	int count;		/* defines range of change w. first_keycode*/
656 }
657 
658 struct XErrorEvent
659 {
660 	int type;
661 	Display *display;	/* Display the event was read from */
662 	XID resourceid;		/* resource id */
663 	c_ulong  serial;	/* serial number of failed request */ 
664 	ubyte error_code;	/* error code of failed request */
665 	ubyte request_code;	/* Major op-code of failed request */
666 	ubyte minor_code;	/* Minor op-code of failed request */
667 }
668 
669 struct XAnyEvent
670 {
671 	int type;
672 	c_ulong  serial;	/* # of last request processed by server */ 
673 	Bool send_event;	/* true if this came from a SendEvent request */
674 	Display *display;/* Display the event was read from */
675 	Window window;	/* window on which event was requested in event mask */
676 }
677 
678 
679 /***************************************************************
680  *
681  * GenericEvent.  This event is the standard event for all newer extensions.
682  */
683 
684 struct XGenericEvent
685 {
686     int            type;         /* of event. Always GenericEvent */
687     c_ulong   serial;       /* # of last request processed */ 
688     Bool           send_event;   /* true if from SendEvent request */
689     Display        *display;     /* Display the event was read from */
690     int            extension;    /* major opcode of extension that caused the event */
691     int            evtype;       /* actual event type. */
692 }
693 
694 struct XGenericEventCookie
695 {
696     int            type;         /* of event. Always GenericEvent */
697     c_ulong   serial;       /* # of last request processed */ 
698     Bool           send_event;   /* true if from SendEvent request */
699     Display        *display;     /* Display the event was read from */
700     int            extension;    /* major opcode of extension that caused the event */
701     int            evtype;       /* actual event type. */
702     uint   cookie;
703     void           *data;
704 }
705 
706 struct XComposeStatus
707 {
708     XPointer compose_ptr;
709     int chars_matched;
710 }
711 
712 enum {
713     NoEventMask          = 0,
714     KeyPressMask         = 1L << 0,
715     KeyReleaseMask       = 1L << 1,
716     KeymapStateMask      = 1L << 14,
717     ExposureMask         = 1L << 15,
718     VisibilityChangeMask = 1L << 16,
719     ResizeRedirectMask   = 1L << 18,
720     ButtonPressMask      = 1L << 2,
721     ButtonReleaseMask    = 1L << 3,
722     EnterWindowMask      = 1L << 4,
723     LeaveWindowMask      = 1L << 5,
724     PointerMotionMask    = 1L << 6,
725     PointerMotionHintMask= 1L << 7,
726     Button1MotionMask    = 1L << 8,
727     Button2MotionMask    = 1L << 9,
728 }
729 
730 enum KeyPress		= 2;
731 enum KeyRelease		= 3;
732 enum ButtonPress		= 4;
733 enum ButtonRelease		= 5;
734 enum MotionNotify		= 6;
735 enum EnterNotify		= 7;
736 enum LeaveNotify		= 8;
737 enum FocusIn			= 9;
738 enum FocusOut		= 10;
739 enum KeymapNotify		= 11;
740 enum Expose			= 12;
741 enum GraphicsExpose		= 13;
742 enum NoExpose		= 14;
743 enum VisibilityNotify	= 15;
744 enum CreateNotify		= 16;
745 enum DestroyNotify		= 17;
746 enum UnmapNotify		= 18;
747 enum MapNotify		= 19;
748 enum MapRequest		= 20;
749 enum ReparentNotify		= 21;
750 enum ConfigureNotify		= 22;
751 enum ConfigureRequest	= 23;
752 enum GravityNotify		= 24;
753 enum ResizeRequest		= 25;
754 enum CirculateNotify		= 26;
755 enum CirculateRequest	= 27;
756 enum PropertyNotify		= 28;
757 enum SelectionClear		= 29;
758 enum SelectionRequest	= 30;
759 enum SelectionNotify		= 31;
760 enum ColormapNotify		= 32;
761 enum ClientMessage		= 33;
762 enum MappingNotify		= 34;
763 enum GenericEvent		= 35;
764 enum LASTEvent		= 36	/* must be bigger than any event # */;
765 
766 enum InputOutput = 1;
767 enum InputOnly   = 2;
768 enum CWX		   = (1<<0);
769 enum CWY	       = (1<<1);
770 enum CWWidth	   = (1<<2);
771 enum CWHeight	   = (1<<3);
772 enum CWBorderWidth = (1<<4);
773 enum CWSibling	   = (1<<5);
774 enum CWStackMode   = (1<<6);
775 
776 enum CWBackPixmap		= (1L<<0);
777 enum CWBackPixel		= (1L<<1);
778 enum CWBorderPixmap		= (1L<<2);
779 enum CWBorderPixel		= (1L<<3);
780 enum CWBitGravity		= (1L<<4);
781 enum CWWinGravity		= (1L<<5);
782 enum CWBackingStore		= (1L<<6);
783 enum CWBackingPlanes	= (1L<<7);
784 enum CWBackingPixel		= (1L<<8);
785 enum CWOverrideRedirect	= (1L<<9);
786 enum CWSaveUnder		= (1L<<10);
787 enum CWEventMask		= (1L<<11);
788 enum CWDontPropagate	= (1L<<12);
789 enum CWColormap		    = (1L<<13);
790 enum CWCursor		    = (1L<<14);
791 
792 union XEvent 
793 {
794     int type;		/* must not be changed; first element */
795 	XAnyEvent xany;
796 	XKeyEvent xkey;
797 	XButtonEvent xbutton;
798 	XMotionEvent xmotion;
799 	XCrossingEvent xcrossing;
800 	XFocusChangeEvent xfocus;
801 	XExposeEvent xexpose;
802 	XGraphicsExposeEvent xgraphicsexpose;
803 	XNoExposeEvent xnoexpose;
804 	XVisibilityEvent xvisibility;
805 	XCreateWindowEvent xcreatewindow;
806 	XDestroyWindowEvent xdestroywindow;
807 	XUnmapEvent xunmap;
808 	XMapEvent xmap;
809 	XMapRequestEvent xmaprequest;
810 	XReparentEvent xreparent;
811 	XConfigureEvent xconfigure;
812 	XGravityEvent xgravity;
813 	XResizeRequestEvent xresizerequest;
814 	XConfigureRequestEvent xconfigurerequest;
815 	XCirculateEvent xcirculate;
816 	XCirculateRequestEvent xcirculaterequest;
817 	XPropertyEvent xproperty;
818 	XSelectionClearEvent xselectionclear;
819 	XSelectionRequestEvent xselectionrequest;
820 	XSelectionEvent xselection;
821 	XColormapEvent xcolormap;
822 	XClientMessageEvent xclient;
823 	XMappingEvent xmapping;
824 	XErrorEvent xerror;
825 	XKeymapEvent xkeymap;
826 	XGenericEvent xgeneric;
827 	XGenericEventCookie xcookie;
828 	long[24] pad;
829 }
830 
831 
832 
833 
834 version(SharedX11)
835 {
836 	extern(C) nothrow @nogc __gshared
837 	{
838 		Display*  function(const(char)* display_name) XOpenDisplay;
839 		int function(Display* display) XCloseDisplay;
840 		
841 		Window function (Display *display,
842 		Window parent,
843 		int x, int y,
844 		uint width, uint height,
845 		uint border_width,
846 		ulong border,
847 		ulong background) XCreateSimpleWindow;
848 
849 		Window function(
850 		Display* display,
851 		Window parent,
852 		int x, int y,
853 		uint width, uint height,
854 		uint border_width,
855 		int depth,
856 		uint _class,
857 		Visual* visual,
858 		c_ulong  valuemask, 
859 		XSetWindowAttributes* attributes
860 		) XCreateWindow;
861 		int function(Display *display, Window w) XClearWindow;
862 		int function(Display *display, Window w) XMapRaised;
863 		int function(Display *display, XEvent *event_return) XNextEvent;
864 		int function(Display *display, XEvent *event_return) XPeekEvent;
865 		int function(Display *display, Window w) XDestroyWindow;
866 		int function(Display *display, Window w, long event_mask) XSelectInput;
867 		Status function(Display* display, Window w, XWindowAttributes* win_attr) XGetWindowAttributes;
868 		int function(XMappingEvent *event_map) XRefreshKeyboardMapping;
869 		int function(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out) XLookupString;
870 		KeySym function(Display* display, typeof(XKeyEvent.keycode) keycode, int index) XKeycodeToKeysym;
871 		Colormap function(Display *display, Window w, Visual *visual, int alloc) XCreateColormap;
872 		int function(Display *display, Window w, const(char)* window_name) XStoreName;
873 		int function(Display* display,Window w,uint value_mask,XWindowChanges* values) XConfigureWindow;
874 		int function(void* data) XFree;
875 		int function(Display* display,Colormap colormap) XFreeColormap;
876 		int function (Display *display) XFlush;
877 		int function (Display *display, Bool discard) XSync;
878 		int function(XErrorHandler handler) XSetErrorHandler;
879 		Atom function (Display *display, const(char)* atom_name, Bool only_if_exists) XInternAtom;
880 		Status function(Display* display, Window w, Atom* protocols, int count) XSetWMProtocols;
881 		int function(Display* display) XPending;
882 	
883 
884 
885 		//GL
886 		XVisualInfo* function(Display* display,int screen,int* attribList) glXChooseVisual;
887 		GLXContext function(Display* display, XVisualInfo* vis, GLXContext shareList, Bool direct) glXCreateContext;
888 		Bool function(Display* dpy,GLXDrawable drawable, GLXContext ctx) glXMakeCurrent;
889 		void function(Display* dpy, GLXDrawable drawable) glXSwapBuffers;
890 		void function(Display* dpy, GLXContext ctx) glXDestroyContext;
891 		Bool function (Display * dpy, int * major,int * minor) glXQueryVersion;
892 		GLXFBConfig* function(Display * dpy,int screen,const(int)* attrib_list,int* nelements) glXChooseFBConfig;
893 		const (char)* function(	Display * dpy,int screen) glXQueryExtensionsString;
894 		void* function (const GLubyte* procName) glXGetProcAddressARB;
895 		GLXContext function(Display * dpy, GLXFBConfig config,int render_type,GLXContext share_list,Bool direct)  glXCreateNewContext;
896 		XVisualInfo* function(Display * dpy, GLXFBConfig config) glXGetVisualFromFBConfig;
897 		int function(Display* dpy, GLXFBConfig config,int attribute,int* value) glXGetFBConfigAttrib;
898 		Bool function(Display* dpy, GLXContext context) glXIsDirect;
899 
900 
901 	}
902 }
903 else
904 {
905 	extern(C) nothrow @nogc __gshared extern
906 	{
907 		Display* XOpenDisplay(const(char)* display_name);
908 		int  XCloseDisplay(Display* display);
909 		
910 		Window  XCreateSimpleWindow(Display* display,
911 		Window parent,
912 		int x, int y,
913 		uint width, uint height,
914 		uint border_width,
915 		ulong border,
916 		ulong background);
917 
918 		Window XCreateWindow (
919 		Display* display,
920 		Window parent,
921 		int x, int y,
922 		uint width, uint height,
923 		uint border_width,
924 		int depth,
925 		uint _class,
926 		Visual* visual,
927 		c_ulong  valuemask, 
928 		XSetWindowAttributes* attributes
929 		);
930 		int  XClearWindow(Display *display, Window w);
931 		int  XMapRaised(Display *display, Window w);
932 		int  XNextEvent(Display *display, XEvent *event_return);
933 		int  XPeekEvent(Display *display, XEvent *event_return);
934 		int  XDestroyWindow(Display *display, Window w);
935 		int  XSelectInput(Display *display, Window w, long event_mask);
936 		Status  XGetWindowAttributes(Display* display, Window w, XWindowAttributes* win_attr);
937 		int  XRefreshKeyboardMapping(XMappingEvent *event_map);
938 		int  XLookupString(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out);
939 		KeySym XKeycodeToKeysym(Display* display, typeof(XKeyEvent.keycode) keycode, int index);
940 		
941 		Colormap  XCreateColormap(Display *display, Window w, Visual *visual, int alloc);
942 		int  XStoreName(Display *display, Window w, const(char)* window_name);
943 		int  XConfigureWindow(Display* display,Window w,uint value_mask,XWindowChanges* values);
944 		int  XFree(void* data);
945 		int  XFreeColormap(Display* display,Colormap colormap);
946 		int  XFlush(Display *display);
947 		int  XSync(Display *display, Bool discard);
948 		int  XSetErrorHandler(XErrorHandler handler);
949 		Atom XInternAtom(Display *display, const(char)* atom_name, Bool only_if_exists);
950 		Status XSetWMProtocols(Display* display, Window w, Atom* protocols, int count);
951 		int XPending(Display* display);
952 
953 
954 		//GL
955 		XVisualInfo* glXChooseVisual(Display* display,int screen,int* attribList);
956 		GLXContext  glXCreateContext(Display* display, XVisualInfo* vis, GLXContext shareList, Bool direct);
957 		Bool  glXMakeCurrent(Display* dpy,GLXDrawable drawable, GLXContext ctx);
958 		void  glXSwapBuffers(Display* dpy, GLXDrawable drawable);
959 		void  glXDestroyContext(Display* dpy, GLXContext ctx);
960 		Bool glXQueryVersion(Display * dpy, int * major,int * minor);
961 		GLXFBConfig* glXChooseFBConfig(Display * dpy,int screen,const(int)* attrib_list,int* nelements);
962 		const (char)* glXQueryExtensionsString(	Display * dpy,int screen);
963 		void *glXGetProcAddressARB(const(GLubyte)* procName);
964 		GLXContext glXCreateNewContext(	Display * dpy, GLXFBConfig config,int render_type,GLXContext share_list,Bool direct);
965 		XVisualInfo* glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config);
966 		int glXGetFBConfigAttrib(Display* dpy, GLXFBConfig config,int attribute,int* value);
967 		Bool glXIsDirect(Display* dpy, GLXContext context);
968 
969 
970 	}
971 }
972 version(SharedX11):
973 
974 package void* dl;
975 
976 void loadX11()
977 {
978     static bool hasStart = false;
979     if(!hasStart)
980     {
981         import core.sys.linux.dlfcn;
982         import core.stdc.stdio:printf;
983 		//There is some cases which seems X11 is able to load even when the 'dl' is null.
984         dl = dlopen("X11", RTLD_LAZY); 
985 		// if(dl == null)
986 		// {
987 		// 	printf("Could not find libX11.so\n");
988 		// 	return;
989 		// }
990         void* load_dl_func(const(char*) func)
991         {
992             void* ret =  dlsym(dl, func);
993             if(ret == null)
994                 printf("Could not load %s:\n", func);
995             return ret;
996         }
997 
998         XOpenDisplay = cast(typeof(XOpenDisplay))load_dl_func("XOpenDisplay");
999         XCloseDisplay = cast(typeof(XCloseDisplay))load_dl_func("XCloseDisplay");
1000         XCreateSimpleWindow = cast(typeof(XCreateSimpleWindow))load_dl_func("XCreateSimpleWindow");
1001         XCreateWindow = cast(typeof(XCreateWindow))load_dl_func("XCreateWindow");
1002         XClearWindow = cast(typeof(XClearWindow))load_dl_func("XClearWindow");
1003         XMapRaised = cast(typeof(XMapRaised))load_dl_func("XMapRaised");
1004         XNextEvent = cast(typeof(XNextEvent))load_dl_func("XNextEvent");
1005         XPeekEvent = cast(typeof(XPeekEvent))load_dl_func("XPeekEvent");
1006         XDestroyWindow = cast(typeof(XDestroyWindow))load_dl_func("XDestroyWindow");
1007         XSelectInput = cast(typeof(XSelectInput))load_dl_func("XSelectInput");
1008         XGetWindowAttributes = cast(typeof(XGetWindowAttributes))load_dl_func("XGetWindowAttributes");
1009         XRefreshKeyboardMapping = cast(typeof(XRefreshKeyboardMapping))load_dl_func("XRefreshKeyboardMapping");
1010         XLookupString = cast(typeof(XLookupString))load_dl_func("XLookupString");
1011 		XKeycodeToKeysym = cast(typeof(XKeycodeToKeysym))load_dl_func("XKeycodeToKeysym");
1012         XCreateColormap = cast(typeof(XCreateColormap))load_dl_func("XCreateColormap");
1013         XStoreName = cast(typeof(XStoreName))load_dl_func("XStoreName");
1014         XConfigureWindow = cast(typeof(XConfigureWindow))load_dl_func("XConfigureWindow");
1015         XFree = cast(typeof(XFree))load_dl_func("XFree");
1016         XFreeColormap = cast(typeof(XFreeColormap))load_dl_func("XFreeColormap");
1017         XFlush = cast(typeof(XFlush))load_dl_func("XFlush");
1018         XSync = cast(typeof(XSync))load_dl_func("XSync");
1019         XSetErrorHandler = cast(typeof(XSetErrorHandler))load_dl_func("XSetErrorHandler");
1020         XInternAtom = cast(typeof(XInternAtom))load_dl_func("XInternAtom");
1021         XSetWMProtocols = cast(typeof(XSetWMProtocols))load_dl_func("XSetWMProtocols");
1022         XPending = cast(typeof(XPending))load_dl_func("XPending");
1023 		
1024 
1025         glXChooseVisual = cast(typeof(glXChooseVisual))load_dl_func("glXChooseVisual");
1026         glXCreateContext = cast(typeof(glXCreateContext))load_dl_func("glXCreateContext");
1027         glXMakeCurrent = cast(typeof(glXMakeCurrent))load_dl_func("glXMakeCurrent");
1028         glXSwapBuffers = cast(typeof(glXSwapBuffers))load_dl_func("glXSwapBuffers");
1029         glXDestroyContext = cast(typeof(glXDestroyContext))load_dl_func("glXDestroyContext");
1030 		glXQueryVersion = cast(typeof(glXQueryVersion))load_dl_func("glXQueryVersion");
1031 		glXChooseFBConfig = cast(typeof(glXChooseFBConfig))load_dl_func("glXChooseFBConfig");
1032 		glXQueryExtensionsString = cast(typeof(glXQueryExtensionsString))load_dl_func("glXQueryExtensionsString");
1033 		glXGetProcAddressARB = cast(typeof(glXGetProcAddressARB))load_dl_func("glXGetProcAddressARB");
1034 		glXCreateNewContext = cast(typeof(glXCreateNewContext))load_dl_func("glXCreateNewContext");
1035 		glXGetVisualFromFBConfig = cast(typeof(glXGetVisualFromFBConfig))load_dl_func("glXGetVisualFromFBConfig");
1036 		glXGetFBConfigAttrib = cast(typeof(glXGetFBConfigAttrib))load_dl_func("glXGetFBConfigAttrib");
1037 		glXIsDirect = cast(typeof(glXIsDirect))load_dl_func("glXIsDirect");
1038 
1039 
1040         hasStart = true;
1041     }
1042 }
1043 
1044 void unloadX11()
1045 {
1046 	import core.stdc.stdio;
1047 	import core.sys.linux.dlfcn;
1048 	if(dl != null && !dlclose(dl))
1049 		printf("Could not unload X11.\n");
1050 }